package com.tengju.bff.interfaces.manage.crm.user;

import com.tengju.bff.interfaces.manage.crm.user.vo.AddSurveyConfigForm;
import com.tengju.bff.interfaces.manage.crm.user.vo.EditSurveyConfigForm;
import com.tengju.bff.interfaces.manage.star.vo.SurveyConfigVO;
import com.tengju.bff.interfaces.manage.crm.user.assembler.ArchiveAssembler;
import com.tengju.bff.interfaces.manage.crm.user.facade.ArchiveManageFacade;
import com.tengju.bff.interfaces.manage.crm.user.vo.ArchiveContentForm;
import com.tengju.bff.interfaces.manage.crm.user.vo.ArchiveContentVo;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.servlet.NotTokenValid;
import com.tengju.support.domain.annotation.BizLog;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.support.domain.annotation.BizLogSysName;
import com.tengju.user.application.archives.ArchiveService;
import com.tengju.user.application.shared.ApplicationException;
import com.tengju.user.domain.model.survey.SurveyConfigInfo;
import com.tengju.user.domain.model.survey.SurveyContent;
import com.tengju.user.domain.model.survey.SurveyId;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.shared.DomainException;
import com.tengju.user.domain.shared.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@RestController
@Api(tags = "档案管理操作")
@RequestMapping(value = "manage/crm/archive")
@Slf4j
@ManageEntrance
public class ArchiveManageController {

    private ArchiveManageFacade archiveManageFacade;
    private ArchiveService archiveService;

    @Autowired
    public ArchiveManageController(ArchiveManageFacade archiveManageFacade, ArchiveService archiveService) {
        this.archiveManageFacade = archiveManageFacade;
        this.archiveService = archiveService;
    }

    @ApiOperation(value = "获取服务商档案配置", notes = "获取服务商档案配置")
    @RequestMapping(value = "getArchiveConfig", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<SurveyConfigVO> getSurveyConfig(@NotNull(message = "问卷id不能为空") Long surveyId,
                                                       @NotNull(message = "用户idCode不能为空") Long idCode) {
        try {
            return ApiResponse.newSuccess(
                    archiveManageFacade.getSurveyConfig(new SurveyId(surveyId)
                            ,new UserIdCode(idCode)));
        } catch (DomainException | ApplicationException e) {
            log.warn("获取服务商档案配置失败,surveyId:{},idCode:{}",surveyId,idCode,e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @BizLog(sysName = BizLogSysName.TENGJU_CRM)
    @ApiOperation(value = "提交档案", notes = "提交档案")
    @RequestMapping(value = "submit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> submit(@Valid @RequestBody ArchiveContentForm form) {
        try {
            SurveyContent surveyContent = ArchiveAssembler.contentVOToModel(form);
            return ApiResponse.newSuccess(archiveService.submit(surveyContent));
        } catch (DomainException | ApplicationException e) {
            log.warn("提交档案失败,surveyId:{},idCode:{}",form.getSurveyId(),form.getIdCode(),e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "档案信息", notes = "档案信息")
    @RequestMapping(value = "getContent", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<List<ArchiveContentVo>> getContent(@NotNull(message = "问卷id不能为空") Long surveyId,
                                                          @NotNull(message = "用户idCode不能为空") Long idCode) {
        try {
            return ApiResponse.newSuccess(archiveManageFacade.getContent(new SurveyId(surveyId),new UserIdCode(idCode)));
        } catch (DomainException | ApplicationException e) {
            log.error("查询档案信息失败,surveyId:{},idCode:{}",surveyId,idCode,e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "问卷调查配置", notes = "问卷调查配置")
    @RequestMapping(value = "addConfig", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<Boolean> addConfig(@Valid @RequestBody AddSurveyConfigForm form) {

        try {
            SurveyConfigInfo surveyConfigInfo = ArchiveAssembler.contentVOToModel(form);
            return ApiResponse.newSuccess(archiveService.saveConfig(surveyConfigInfo));
        } catch (DomainException | ApplicationException e) {
            log.error("问卷调查配置失败,form:{}",form,e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "修改问卷调查配置", notes = "修改问卷调查配置")
    @RequestMapping(value = "editConfig", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<Boolean> editConfig(@Valid @RequestBody EditSurveyConfigForm form) {

        try {
            SurveyConfigInfo surveyConfigInfo = ArchiveAssembler.contentVOToModel(form);
            return ApiResponse.newSuccess(archiveService.updateConfig(form.getId(),surveyConfigInfo));
        } catch (DomainException | ApplicationException e) {
            log.error("问卷调查配置失败,form:{}",form,e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

}
